package org.zerg.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zerg.dao.AddressDao;
import org.zerg.dao.OrderDao;
import org.zerg.dao.UserDao;
import org.zerg.dto.Page;
import org.zerg.entity.Address;
import org.zerg.entity.Order;
import org.zerg.entity.User;
import org.zerg.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private AddressDao addressDao;
	
	@Autowired
	private OrderDao orderDao;

	public List<User> getUser(int page) {
		//获取用户总数
		int total = userDao.getUserTotal();
		if((page-1)*15>total){
			return null;
		}
		List<User> list = userDao.getUserAllPage((page-1)*15);
		return list;
	}
	
	public Page pageMessage(int page) {
		int total = userDao.getUserTotal();
		double to = total;
		int totalPage = (int) Math.ceil(to/15);
		Page pa = new Page(page,totalPage,total,15);
		return pa;
	}

	public Address getAddress(int id) {
		Address address = addressDao.getAddress(id);
		return address;
	}

	public List<Order> getOrder(int id, int page) {
		//算出总记录数，做分页
		int total = orderDao.getOrderTotalByUser(id);
		if((page-1)*15>total){
			return null;
		}
		List<Order> list = orderDao.getOrderByUserPage((page-1)*15,id);
		return list;
	}

	public Page pageMessageForOr(int page,int id) {
		int total = orderDao.getOrderTotalByUser(id);
		double to = total;
		int totalPage = (int) Math.ceil(to/15);
		Page pa = new Page(page,totalPage,total,15);
		return pa;
	}

}
